/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-10         luwei
 */

package com.tianci.appstore.ui.mainpage;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.control.SkyMainPageController.OnPageChangeListener;
import com.tianci.appstore.ui.control.SkyMyAppController;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;
import com.tianci.appstore.ui.widget.SkyGridAdapter;
import com.tianci.appstore.ui.widget.SkyGridAdapter.AdapterType;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class SkyMyAppPage extends RelativeLayout implements OnPageChangeListener {
    protected Context mContext = null;
    protected SkyMyAppController mController = null;
    protected List<View> mLayouts = new ArrayList<View>();
    protected SkyAppPager mPager = null;
    private ImageView mLeftArrow, mRightArrow;
    protected TextView pageNumberText;
    private TextView tipText;

    private boolean isChildrenMode = false;

    public SkyMyAppPage(Context context, Direction direction) {
        this(context, direction, false);
    }

    public SkyMyAppPage(Context context, Direction direction, boolean isChildrenMode) {
        super(context);
        this.mContext = context;
        addContent(direction);
        this.isChildrenMode = isChildrenMode;
        initController();
        mController.setChildrenMode(isChildrenMode);
        mController.setMyAppPage(this);
        mPager.addView(initMyAppPage());
        initPageNoView();
        showPageNumAndArrow(false);
    }

    protected void initController() {
        mController = new SkyMyAppController(mContext);
    }

    public void addContent(Direction direction) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mPager = new SkyAppPager(mContext, direction);
        mPager.setLayoutParams(params);
        addView(mPager);
    }

    public SkyMyAppController getController() {
        return mController;
    }

    protected SkyCustomLayout initMyAppPage() {
        SkyCustomLayout layout = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
        layout.setBlockNum(3, 4);
        if (isChildrenMode) {
            layout.setBlockSize(SkyConstants.Div(613), SkyConstants.Div(406));
            layout.setVerticalSpace(SkyConstants.Div(-171));
            layout.setHorizontalSpace(SkyConstants.Div(-188));
        } else {
            layout.setBlockSize(SkyConstants.Div(462), SkyConstants.Div(271));
            layout.setVerticalSpace(SkyConstants.Div(-44));
            layout.setHorizontalSpace(SkyConstants.Div(-36));
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);
        if (isChildrenMode)
            layout.setMargin(SkyConstants.Div(21), SkyConstants.Div(282), 0, 0);
        else
            layout.setMargin(SkyConstants.Div(97), SkyConstants.Div(188), SkyConstants.Div(0), 0);

        layout.setBlockDatas(generateLocalAppBlockData());
        SkyGridAdapter mAdapter = new SkyGridAdapter(mContext, CoocaaApplication.mDiv, AdapterType.LIST_TYPE);
        mAdapter.setChildrenMode(isChildrenMode);
        mAdapter.setKeyListener(mController);
        layout.setAdapter(mAdapter);
        mController.setAppPager(mPager);
        return layout;
    }

    private void initPageNoView() {
        mLeftArrow = new ImageView(mContext);
        mRightArrow = new ImageView(mContext);
        if (isChildrenMode) {
            mLeftArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.child_left_arrow));
            mRightArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.child_right_arrow));
        } else {
            mLeftArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.left_arrow));
            mRightArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.right_arrow));
        }
        RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.CENTER_VERTICAL);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        if (isChildrenMode) {
            leftParams.leftMargin = SkyConstants.Div(18);
            rightParams.rightMargin = SkyConstants.Div(18);
        } else {
            leftParams.leftMargin = SkyConstants.Div(36);
            rightParams.rightMargin = SkyConstants.Div(36);
        }

        this.addView(mLeftArrow, leftParams);
        this.addView(mRightArrow, rightParams);

        int colorId = R.color.pageNumberColor;
        if (isChildrenMode)
            colorId = R.color.pageNumberColorChild;
        pageNumberText = UICommon.newTextView(mContext, SkyConstants.Dpi(33), colorId);
        RelativeLayout.LayoutParams pageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        pageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        pageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        pageParams.bottomMargin = SkyConstants.Div(45);
        this.addView(pageNumberText, pageParams);
        mController.setPageChangeListener(this);
    }

    public void showTips(int iconId, String tipStr) {
        if (tipText == null) {
            tipText = new TextView(mContext);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            tipText.setLayoutParams(params);
            tipText.setTextSize(SkyConstants.Dpi(46));
            tipText.setTextColor(Color.WHITE);
            tipText.setCompoundDrawablePadding(SkyConstants.Div(18));
            this.addView(tipText);
        }
        if (iconId != 0) {
            tipText.setCompoundDrawablesWithIntrinsicBounds(
                    SkinManager.getResDrawable(iconId), null, null, null);
        }
        tipText.setText(tipStr);
        tipText.setVisibility(View.VISIBLE);
    }

    public void hideTips() {
        if (tipText != null)
            tipText.setVisibility(View.INVISIBLE);
    }

    public SkyCustomLayout getCurrentChild() {
        return (SkyCustomLayout) mPager.getChildAt(mPager.getDisplayedChild());
    }

    public SkyCustomLayout getPageChildAt(int index) {
        return (SkyCustomLayout) mPager.getChildAt(index);
    }

//    public void addMoreAppPage(int num)
//    {
//        for (int i = 0; i < num; i++)
//        {
//            initMyAppPage();
//        }
//    }

    public void onDestory() {

    }

    public void reSizePager(int totalPage) {
        if (totalPage == 0) {
            if (isChildrenMode)
                showTips(R.drawable.cart_icon, mContext.getResources().getString(R.string.no_children_app));
            else
                showTips(R.drawable.cart_icon, mContext.getResources().getString(R.string.no_my_app));
        } else
            hideTips();
        int temp = totalPage - mPager.getChildCount();
        if (temp > 0) {
            for (int i = 0; i < temp; i++) {
                if (mLayouts.size() == 0) {
                    SkyCustomLayout layout = initMyAppPage();
                    mPager.addView(layout);
                } else {
                    mPager.addView(mLayouts.remove(0), mPager.getChildCount());
                }
            }
        } else {
            for (int i = 0; i < Math.abs(temp); i++) {
                if (mPager.getChildCount() > 0) {
                    View view = mPager.getChildAt(mPager.getChildCount() - 1);
                    mLayouts.add(view);
                    mPager.removeViewAt(mPager.getChildCount() - 1);
                }
            }
        }
        mPager.setInAnimation(null);
        mPager.setOutAnimation(null);
        mPager.setDisplayedChild(0);
    }

    public void showPageNumAndArrow(boolean show) {
        if (show) {
            setViewVisible(mLeftArrow, View.VISIBLE);
            setViewVisible(mRightArrow, View.VISIBLE);
            setViewVisible(pageNumberText, View.VISIBLE);
        } else {
            setViewVisible(mLeftArrow, View.GONE);
            setViewVisible(mRightArrow, View.GONE);
            setViewVisible(pageNumberText, View.GONE);
        }
    }

    private void setViewVisible(View view, int visible) {
        if (view.getVisibility() != visible)
            view.setVisibility(visible);
    }

    @Override
    public void onPageChange(int currentPage, int totalPage) {
        LogUtils.d("update page num is " + currentPage + " and " + totalPage);
        setPageNoIndex(currentPage, totalPage);
    }

    private void setPageNoIndex(int currentPage, int totalPage) {
        if (pageNumberText != null)
            pageNumberText.setText(String.format("%d/%d%s",
                    (currentPage + 1), totalPage, mContext.getResources().getString(R.string.page)));
        pageNumberText.setVisibility(View.VISIBLE);
        if (currentPage > 0) {
            mLeftArrow.setVisibility(View.VISIBLE);
        } else {
            mLeftArrow.setVisibility(View.INVISIBLE);
        }
        if (currentPage < totalPage - 1) {
            mRightArrow.setVisibility(View.VISIBLE);
        } else {
            mRightArrow.setVisibility(View.INVISIBLE);
        }
    }

    private List<BlockData> generateLocalAppBlockData() {
        List<BlockData> datas = new ArrayList<BlockData>();
        for (int i = 0; i < 12; i++) {
            BlockData data = new BlockData();
            data.mColNum = 1;
            data.mRowNum = 1;
            data.mBlock = new int[]{i};
            if (i == 0 || i == 4 || i == 8) {
                data.mTurnPage = new boolean[]{true, false, false, false};
            } else if (i == 3 || i == 7 || i == 11) {
                data.mTurnPage = new boolean[]{false, false, true, false};
            } else {
                data.mTurnPage = new boolean[]{false, false, false, false};
            }
            datas.add(data);
        }
        return datas;
    }
}
